<?php
	require_once "config.php";
    $id = !empty($_GET['id']) ? $_GET['id'] : '0';
    //确定是外卖商户并且绑定了经营范围
    $merchantInfo = $mysql->query('select * from dyspw_member  where id = '.$id.' and is_vip = 3 and sort = 92');
    $merchantInfo = $merchantInfo->fetch_assoc();
    if(!($merchantInfo && $merchantInfo['industry_type'])){
        header("Location: shop_goods_list.php?id=".$id);
    }
    $cate=$mysql->query("select id,classname from dyspw_takeaway where parentid = ".$merchantInfo['industry_type'] ." order by id asc");
    $cate = $cate->fetch_all(MYSQLI_ASSOC);
    $cate = array_column($cate,NULL,'id');
    //商品信息
	$goods_sql = $mysql->query("select * from dyspw_goods where u_id = $id  ORDER BY tid asc,time desc");
	$goods_list = $goods_sql->fetch_all(MYSQLI_ASSOC);
 ?>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	<title>商户中心</title>
	<script src="js/mui.min.js"></script>
    <script src="js/jquery-2.2.4.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="//webapi.amap.com/maps?v=1.4.1&key=fedad41056b27c3e8cc5eaca216434e9"></script>
    <link href="css/mui.min.css" rel="stylesheet" />
    <link href="css/cate.css" rel="stylesheet" />
    <link href="css/index.css" rel="stylesheet" />
	<link rel="stylesheet" href="css/core.css">
    <script>
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?cb5d49b07e0a753dc8ee27ba2c73495d";
          var s = document.getElementsByTagName("script")[0];
          s.parentNode.insertBefore(hm, s);
        })();
    </script>
</head>
<body>
<style>
    .container .list .item .content .address .add{max-width: 100%}
    .merchantBox{height: calc(100vh - 180px);width: 100%;overflow: hidden }
    .cate_left{width:25%;height: 100%;background:#f7f7f7;position: relative;overflow-y: auto;float: left;}
    .cate_left::-webkit-scrollbar {width:0;height: 1px;}
    .cate_left::-webkit-scrollbar-thumb {border-radius: 10px;background: #717171;}
    .cate_left::-webkit-scrollbar-track {border-radius:10px;background:#ededed; }
    .cate_left p{text-align: center;font-size: 14px;padding: 11px 0;width: calc(100% - 2px)}
    .cate_left p.on{color: #ff8c14;background: #fff}
    .cate_right{width: 75%;height: 100%; position: relative;overflow-y: auto; float: left;padding: 0 4% 80px}
    .cate_right::-webkit-scrollbar {width:0;height: 1px;}
    .cate_right::-webkit-scrollbar-thumb {border-radius: 10px;background: #717171;}
    .cate_right::-webkit-scrollbar-track {border-radius:10px;background:#ededed; }
    .cate_right ul li{margin-bottom:4%;height: auto;overflow: hidden;position: relative}
    .goodsPic{width:80px;height:80px;float: left;border-radius: 5px;margin-right: 15px}
    .goodsIntro{float: left;width: calc(100% - 100px)}
    .goodsIntro p{color: #000;font-size: 16px;font-weight: bold;margin-top: 5px;height:52px}
    .goodsIntro font{color: #ff0000;font-weight: bold;margin-top: 5px;}
    .numberBox{position: absolute;right: 0;bottom:3px;width: 100%;text-align: right}
    .numberBox p{text-align: center;width: 20px;line-height: 20px;float: right;color: #fff;background: #ff8c14;border-radius: 50%}
    .numberBox span{display: block;width:20px;line-height: 20px;float: right;margin: 0 3px;text-align: center}
    .hideBox{display: none}
    .onShow{display: block}
    .sumbitBox{position: fixed;bottom: 10px;left: 0;width: 100%}
    .sumbitBox>a{float: left;width: 25%;position: relative;display: block;height: 56px;min-width: 80px}
    .sumbitBox>a:after{content: '';display: block;right: 0;position: absolute;z-index: -1;height: 66px;top: -10px;width: 80%;background: #8d97b2;border-radius: 50% 0 0 50%;max-width: 100px;}
    .sumbitBox>a img{height:46px;position: absolute;right:3%;}
    .sumbitRight{margin-left: 10px;width: calc(75% - 30px);background: #8D97B2;border-radius: 0 40px 40px 0;height: 66px;float: left;margin-top: -10px}
    .sumbitRight a{float: right;color: #fff;background: #fb8c19;border-radius: 15px;padding: 5px 10px;margin: 18px 15px 0 0}
    .sumbitRight>p{float: left;color: #ff8c14;margin:23px 0 0 15px}
    .sumbitRight>p span{display: block;font-weight: bold;font-size: 20px}
    .sumbitRight>p font{display: block;font-size: 12px}
    .cateMeau{color: #ff8c14;padding:4% 0}
</style>
<div class="container">
    <div class="top" style="height: auto!important;">
        <div class="header mui-row">
            <div class="left mui-col-xs-6">
                <span class="mui-icon mui-icon-arrowleft" onclick="window.location.href='/xiaochengxu/shop_list.php?sort=92'"></span>
                <img src="./images/logo.gif">
                <span class="title">生活</span>
            </div>
        </div>
    </div>

    <div class="list" style="border-bottom: 1px solid #dcdddd">
        <div class="item mui-row">
            <div class="logo mui-col-xs-4">
                <img src="<?php echo $merchantInfo['avatar'] ?>">
            </div>
            <div class="content  mui-col-xs-8">
                <div class="shopName">
                    <span class="name mui-ellipsis"><?php echo $merchantInfo['username'] ?></span>
                </div>
                <div class="stars">
                    <img src="images/star.jpg">
                    <img src="images/star.jpg">
                    <img src="images/star.jpg">
                    <img src="images/star.jpg">
                    <img src="images/star.jpg">
                    <span>5.00</span>
                </div>
                <div class="address" style="line-height: 20px;">
                    <div class="add mui-ellipsis">
                        <img src="images/location.jpg">
                        <div class="location "><?php echo $merchantInfo['shop_add'] ?></div>
                    </div>
                </div>
                <div class="price" style="margin-top: 5px;">
                    <span class="mark">介绍：</span>
                    <div class="info" style="height: 20px;line-height: 20px;overflow: hidden;"><?php if(!empty($merchantInfo['intro'])){echo $merchantInfo['intro'];}else{ echo '暂无介绍';} ?></div>
                </div>
            </div>
        </div>
    </div>

<!-- 外卖商品分类   -->
    <div class="merchantBox">
        <div class="cate_left">
            <p class="on"><a href="#miao<?php echo reset($cate)['id'] ?>">全部</a></p>
            <?php  foreach ($cate as $val){  ?>
                 <p><a href="#miao<?php echo $val['id']?>"><?php echo $val['classname']?></a></p>
            <?php } ?>
            <p></p>
            <p></p>
            <p></p>
            <p></p>
        </div>

        <div class="cate_right">
            <ul>
                <?php $cateArr=[]; foreach ($goods_list as $key => $val) {?>
                    <li gid="<?php echo $val['id'] ?>" >
                        <?php if(!in_array($val['tid'],$cateArr)){
                            $cateArr[]=$val['tid'];
                            ?>
                            <div class="cateMeau"  id="miao<?php echo $val['tid']?>"><? echo $cate[$val['tid']]['classname']?></div>
                        <?php }?>
                        <div class="goodsPic" style="background:url(<?php echo $val['img'] ?>)no-repeat center;background-size: 100% auto;"></div>
                        <div class="goodsIntro">
                            <p><?php echo $val['title'] ?></p>
                            <font>¥ <label><?php echo empty($val['group_money']) ?  $val['money'] : $val['group_money'] ?></label></font>
                        </div>
                        <div class="numberBox">
                            <p class="add" onclick="addNum(this)">+</p>
                            <div class="hideBox">
                                <span>0</span>
                                <p class="del" onclick="delNum(this)">-</p>
                            </div>
                        </div>
                    </li>
                <?php }?>
            </ul>
        </div>
    </div>

    <div class="sumbitBox">
        <a href="tel:<?php echo $merchantInfo['mobile']?>"><img src="./images/store.png"></a>
        <div class="sumbitRight">
            <p>
                <span>¥ <label>0.00</label></span>
<!--                <font>另需要配送费¥ <label>19.9</label></font>-->
            </p>
            <a onclick="goPay()">去结算</a>
        </div>
    </div>

</div>

<script>
    var shopId=<?php echo $id?>;
    var shopCar=localStorage.getItem('shopCar') ? JSON.parse(localStorage.getItem('shopCar')) : {};

    $(function () {
        //左侧点击
        $(".cate_left p").click(function () {
            $(this).addClass('on').siblings().removeClass('on')
        });
        //显示购物车商品信息
        shopInfo();
    });

    //添加商品
    function addNum(e){
        var hideBox=$(e).parent().find('.hideBox');
        if(!hideBox.hasClass('onShow')){
            hideBox.addClass('onShow')
        }
        var num=hideBox.find('span').html();
        num ++;
        commonSet(hideBox,num,true);
    }

    //删除商品
    function delNum(e){
        var num=$(e).parent().find('span').html();
        num --;
        if(num == 0){
           $(e).parent().removeClass('onShow');
        }
        commonSet($(e).parent(),num,false);
    }

    //设置商品总金额
    function commonSet(e,num,status){
        $(e).parent().find('span').html(num);
        var price=$(e).parent().parent().find('label').html();
        var sumitObj=$(".sumbitRight p span label");
        var sumitMoney=Number(sumitObj.html());
        if(status === true){
            var money=sumitMoney + Number(price);
        }else {
            var money = sumitMoney - Number(price);
        }
        sumitObj.html(money.toFixed(2));
        //添加本地购物车
        var gid=Number($(e).parent().parent().attr('gid'));
        var buyInfo={'gid':gid,'num':num};

        if(shopCar[shopId]){
            //更新购物车信息
            var gidArray=[];
            $.each(shopCar[shopId],function (index,val) {
                gidArray.push(val['gid']);
            });
            var i=$.inArray(gid,gidArray);
            if(i !== -1){
                //更新
                if(num){
                    shopCar[shopId][i]=buyInfo;
                }else {
                    shopCar[shopId].splice(i,1);
                }
            }else {
                //添加
                shopCar[shopId].push(buyInfo);
            }
        }else {
            //新增购物车信息
            shopCar[shopId]=[buyInfo];
        }
        localStorage.setItem('shopCar',JSON.stringify(shopCar));
    }

    function shopInfo() {
        if(shopCar[shopId]){
            var money=0;
            $(".cate_right ul li").each(function(){
                var liObj=$(this);
                var gid=liObj.attr('gid');
                var price=Number(liObj.find('label').html());
                $.each(shopCar[shopId],function (index,val){
                    if(gid == val.gid){
                        liObj.find('.hideBox').addClass('onShow');
                        liObj.find('.hideBox span').html(val.num);
                        money +=Number(price * val.num);
                    }
                });
            });
            $(".sumbitRight p span label").html(money.toFixed(2));
        }
    }

    //去订单支付页面结算
    function goPay(){
        if(!shopCar[shopId]){
            alert('请选择商品');
            return false;
        }
        window.location.href='food_order.php?id='+shopId;
    }
</script>
</body>
</html>
